package game

import (
	"context"
	"github.com/gin-gonic/gin"
	"github.com/spf13/cast"
	"go.uber.org/zap"
	"google.golang.org/grpc/status"
	"qm_system_server/server/global"
	"qm_system_server/server/model/common/response"
	"qm_system_server/server/proto/grpc/config"
	"qm_system_server/server/service"
)

type QmConfigApi struct {
}

var qmConfigService = service.ServiceGroupApp.GameServiceGroup.QmConfigService

// CreateQmConfig 创建qmConfig表
// @Tags QmConfig
// @Summary 创建qmConfig表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmConfig true "创建qmConfig表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"创建成功"}"
// @Router /qmConfig/createQmConfig [post]
func (qmConfigApi *QmConfigApi) CreateQmConfig(c *gin.Context) {
	type request struct {
		GameHash   string `json:"game_hash" from:"game_hash"`
		PlatformId int64  `json:"platform_id" form:"platform_id"`
		Name       string `json:"name" form:"name"`
		Type       int64  `json:"type" form:"type"`
		Flag       int64  `json:"flag" form:"flag"`
		Desc       string `json:"desc" form:"desc"`
		ServerId   int64  `json:"server_id" form:"server_id"`
	}
	var req request
	_ = c.ShouldBindJSON(&req)

	//获取header中的game-id
	req.GameHash = c.Request.Header.Get("game-hash")
	req.PlatformId = cast.ToInt64(c.Request.Header.Get("platform-id"))

	if req.Name == "" || req.Desc == "" || req.Type == 0 || req.Flag == 0 || req.PlatformId == 0 || req.GameHash == "" {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := config.NewConfigServiceClient(global.ServerGrpc)

	_, err := client.Create(context.Background(), &config.CreateRequest{
		Config: &config.Config{
			GameHash:   req.GameHash,
			PlatformId: req.PlatformId,
			Name:       req.Name,
			Type:       req.Type,
			Flag:       req.Flag,
			Desc:       req.Desc,
			ServerId:   req.ServerId,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("创建成功", c)
}

// DeleteQmConfig 删除qmConfig表
// @Tags QmConfig
// @Summary 删除qmConfig表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmConfig true "删除qmConfig表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"删除成功"}"
// @Router /qmConfig/deleteQmConfig [delete]
func (qmConfigApi *QmConfigApi) DeleteQmConfig(c *gin.Context) {
	ID := c.Query("id")

	//转成int64
	id := cast.ToInt64(ID)

	client := config.NewConfigServiceClient(global.ServerGrpc)

	_, err := client.Delete(context.Background(), &config.DeleteRequest{
		Id: id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}
	response.OkWithMessage("删除成功", c)
}

// DeleteQmConfigByIds 批量删除qmConfig表
// @Tags QmConfig
// @Summary 批量删除qmConfig表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Success 200 {string} string "{"success":true,"data":{},"msg":"批量删除成功"}"
// @Router /qmConfig/deleteQmConfigByIds [delete]
func (qmConfigApi *QmConfigApi) DeleteQmConfigByIds(c *gin.Context) {
	IDs := c.QueryArray("IDs[]")
	if err := qmConfigService.DeleteQmConfigByIds(IDs); err != nil {
		global.GVA_LOG.Error("批量删除失败!", zap.Error(err))
		response.FailWithMessage("批量删除失败", c)
	} else {
		response.OkWithMessage("批量删除成功", c)
	}
}

// UpdateQmConfig 更新qmConfig表
// @Tags QmConfig
// @Summary 更新qmConfig表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data body game.QmConfig true "更新qmConfig表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"更新成功"}"
// @Router /qmConfig/updateQmConfig [put]
func (qmConfigApi *QmConfigApi) UpdateQmConfig(c *gin.Context) {
	type request struct {
		Id       int64  `json:"id" form:"id"`
		Name     string `json:"name" form:"name"`
		Type     int64  `json:"type" form:"type"`
		Flag     int64  `json:"flag" form:"flag"`
		Desc     string `json:"desc" form:"desc"`
		ServerId int64  `json:"server_id" form:"server_id"`
	}

	var req request

	_ = c.ShouldBindJSON(&req)

	if req.Name == "" || req.Desc == "" || req.Type == 0 || req.Flag == 0 || req.Id == 0 {
		response.FailWithMessage("参数不能为空", c)
		return

	}

	client := config.NewConfigServiceClient(global.ServerGrpc)

	_, err := client.Update(context.Background(), &config.UpdateRequest{
		Config: &config.Config{
			Id:       req.Id,
			Name:     req.Name,
			Type:     req.Type,
			Flag:     req.Flag,
			Desc:     req.Desc,
			ServerId: req.ServerId,
		},
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithMessage("更新成功", c)
}

// FindQmConfig 用id查询qmConfig表
// @Tags QmConfig
// @Summary 用id查询qmConfig表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query game.QmConfig true "用id查询qmConfig表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"查询成功"}"
// @Router /qmConfig/findQmConfig [get]
func (qmConfigApi *QmConfigApi) FindQmConfig(c *gin.Context) {
	type request struct {
		Id int64 `json:"id" form:"id"`
	}

	var req request

	c.ShouldBindQuery(&req)

	//判断不能为空
	if req.Id == 0 {
		response.FailWithMessage("参数不能为空", c)
		return
	}

	client := config.NewConfigServiceClient(global.ServerGrpc)
	getResp, err := client.Get(context.Background(), &config.GetRequest{
		Id: req.Id,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(getResp.Config, c)
}

// GetQmConfigList 分页获取qmConfig表列表
// @Tags QmConfig
// @Summary 分页获取qmConfig表列表
// @Security ApiKeyAuth
// @accept application/json
// @Produce application/json
// @Param data query gameReq.QmConfigSearch true "分页获取qmConfig表列表"
// @Success 200 {string} string "{"success":true,"data":{},"msg":"获取成功"}"
// @Router /qmConfig/getQmConfigList [get]
func (qmConfigApi *QmConfigApi) GetQmConfigList(c *gin.Context) {
	type request struct {
		Page       int64  `json:"page" form:"page"`
		PlatformId int64  `json:"platformId" form:"platform_id"`
		GameHash   string `json:"gameHash" form:"game_hash"`
		Name       string `json:"name" form:"name"`
		Type       int64  `json:"type" form:"type"`
		Flag       int64  `json:"flag" form:"flag"`
	}

	var req request

	_ = c.ShouldBindQuery(&req)

	req.PlatformId = cast.ToInt64(c.Request.Header.Get("platform-id"))
	req.GameHash = c.Request.Header.Get("game-hash")

	client := config.NewConfigServiceClient(global.ServerGrpc)

	servers, err := client.List(context.Background(), &config.ListRequest{
		GameHash: req.GameHash,
		Platform: req.PlatformId,
		Name:     req.Name,
		Type:     req.Type,
		Flag:     req.Flag,
		Page:     req.Page,
	})

	if err != nil {
		st, ok := status.FromError(err)
		if ok {
			// 获取 gRPC 错误的描述信息
			response.FailWithMessage(st.Message(), c)
		}
		return
	}

	response.OkWithData(response.PageResult{
		List:  servers.Config,
		Total: servers.Total,
		Page:  int(req.Page),
	}, c)

}
